#include <iostream>
#include <algorithm>
#include <set>

class Person
{
private:
    int age;
    std::string name;

public:
    Person(int age, std::string name) : name(name), age(age) {}
    int getAge() const
    {
        return age;
    }

    std::string getName() const
    {
        return name;
    }

    bool operator<(const Person &p) const
    {
        return age < p.age;
    }

    // bool operator==(const Person &p) const
    // {
    //     return p.age == age && p.name == name;
    // }
};

// struct PersonEqual
// {
//     bool operator()(const Person &p1, const Person &p2) const
//     {
//         return p1.getAge() == p2.getAge() && p1.getName() == p2.getName();
//         // return p1 == p2;
//     }
// };
struct PersonEqual
{
    bool operator()(const Person &p1, const Person &p2) const
    {
        return !(p1.getAge() < p2.getAge() || (p1.getAge() == p2.getAge() && p1.getName() < p2.getName()));
    }
};

std::ostream &operator<<(std::ostream &os, const Person &p)
{
    os << "{" << p.getName() << ", " << p.getAge() << "} ";
    return os;
}

int main(int argc, char const *argv[])
{
    std::set<int> set{};
    // srand(time(0));
    // for (int i = 0; i < 10; i++)
    // {
    //     set.insert(rand() % 100);
    // }
    set.insert(1);
    set.insert(2);
    set.insert(7);
    set.insert(12);
    set.insert(45);
    set.insert(61);
    set.insert(69);
    set.insert(94);

    std::for_each(set.begin(), set.end(), [](const int &i)
                  { std::cout << i << " "; });
    std::cout << std::endl;

    bool result = std::binary_search(set.begin(), set.end(), 8);
    std::cout << result << std::endl;

    std::set<Person> personSet{};

    for (int i = 0; i < 10; i++)
    {
        personSet.insert(Person(i + 18, std::string("Person") + std::to_string(i + 1)));
    }
    std::for_each(personSet.begin(), personSet.end(), [](const Person &p)
                  { std::cout << p << " "; });
    std::cout << std::endl;
    std::cout << personSet.size() << std::endl;

    bool res = std::binary_search(personSet.begin(), personSet.end(), Person(18, "Person1"));
    std::cout << res << std::endl;

    Person searchPerson(18, "Person1");
    std::cout << searchPerson << std::endl;
    auto it = personSet.find(searchPerson);

    if (it != personSet.end())
    {
        std::cout << "Element found in personSet: " << *it << std::endl;
    }
    else
    {
        std::cout << "Element not found in personSet." << std::endl;
    }

    return 0;
}
